package com.solotiger.douban.ui.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.solotiger.douban.MyApplication;
import com.solotiger.douban.R;
import com.solotiger.douban.common.CommonDialogManager;
import com.solotiger.douban.util.ToastUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 所有的Activity的父类
 * BaseFragmentActivity
 */
public abstract class BaseFragmentActivity extends RxAppCompatActivity {

    private CommonDialogManager commonDialogManager = null;

    protected Activity mContext;

    private Toolbar mToolbar;

    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(getLayoutResId());

        mContext = this;

        unbinder = ButterKnife.bind(this);

        initToolBar();
    }

    /**
     * 初始化ToolBar
     */
    private void initToolBar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            mToolbar.setTitle("");
        }
    }

    protected Toolbar getToolbar() {
        return mToolbar;
    }

    protected void setTitle(String title) {
        if (mToolbar != null) {
            mToolbar.setTitle(title);
        }
    }

    protected void setSubTitle(String title) {
        if (mToolbar != null) {
            mToolbar.setSubtitle(title);
        }
    }

    protected void setNavigationOnClickListener(View.OnClickListener listener) {
        if (mToolbar != null) {
            mToolbar.setNavigationOnClickListener(listener);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mContext = null;
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /**
     * 由子类执行后续操作
     *
     * @return
     */
    protected abstract int getLayoutResId();

    public void showMessage(int messageResId, Object... args) {
        ToastUtil.show(MyApplication.getInstance(), MyApplication.getInstance().getString(messageResId, args));
    }

    public void showMessage(CharSequence message) {
        ToastUtil.show(MyApplication.getInstance(), message);
    }

    public CommonDialogManager getCommonDialogManager() {
        if (null == commonDialogManager) {
            commonDialogManager = new CommonDialogManager();
        }
        return commonDialogManager;
    }

    public void loading() {
        getCommonDialogManager().lockLoadData(getSupportFragmentManager());
    }

    public void closeLoading() {
        getCommonDialogManager().unlockLoadData();
    }

}
